//
//  CCQBadgeView.swift
//  ProjectOperationIOS
//
//  Created by 常超群的MacBookPro on 2021/7/2.
//

import UIKit
import SnapKit
public class CCQBadgeView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        loadUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 标题
    private lazy var badgeLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        label.textColor = .white
        label.font = UIFont.systemFont(ofSize: 11)
        label.text = "1"
        return label
    }()
    /// 最大数量
    public var maxBadgeCount = 99
    /// 数量
    public var badgeCount = 0 {
        didSet {
            var text = ""
            if badgeCount <= 0 {
                self.isHidden = true
                text = "\(badgeCount)"
            } else if badgeCount > 0 && badgeCount <= maxBadgeCount {
                self.isHidden = false
                text = "\(badgeCount)"
            } else {
                self.isHidden = false
                text = "\(maxBadgeCount)+"
            }
            badgeLabel.text = text
        }
    }
    /// 字体颜色
    public var badgeTextColor: UIColor = .white {
        didSet {
            badgeLabel.textColor = badgeTextColor
        }
    }
    /// 背景颜色
    public var badgeBackGroundColor: UIColor = .red {
        didSet {
            self.backgroundColor = badgeBackGroundColor
        }
    }
    /// 字体
    public var badgeFont: UIFont = UIFont.systemFont(ofSize: 11) {
        didSet {
            badgeLabel.font = badgeFont
        }
    }
}
extension CCQBadgeView {
    public override func layoutSubviews() {
        super.layoutSubviews()
        self.layer.cornerRadius = self.frame.size.height / 2
    }
    private func loadUI() {
        self.backgroundColor = badgeBackGroundColor
        self.addSubview(badgeLabel)
        badgeLabel.snp.makeConstraints { make in
            make.edges.equalTo(UIEdgeInsets(top: 0, left: 4, bottom: 0, right: 4))
        }
    }
}
